package com.scu.controller;

import com.scu.pojo.Notice;
import com.scu.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @RequestMapping("getNotice")
    @ResponseBody
    public HashMap getNotice(){
        List<Notice> noticeList = noticeService.getNotice();
//        int count = notice.size();

        HashMap<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", noticeList);

        return map;
    }

    @RequestMapping("getNoticeDetail")
    @ResponseBody
    public HashMap getNoticeDetail(HttpServletRequest request) {
        int notice_id = Integer.parseInt(request.getParameter("notice_id"));
        Notice notice = noticeService.getNoticeDetail(notice_id);

        HashMap<String,Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "");
        map.put("data", notice);
        return map;
    }

    /**
     * 添加公告
     * @param request
     * @return
     */
    @RequestMapping("addNotice")
    @ResponseBody
    public HashMap addNotice(HttpServletRequest request){
        String notice_title = request.getParameter("notice_title");
        String notice_content = request.getParameter("notice_content");
//        int notice_author_id = Integer.parseInt(request.getParameter("notice_author_id"));
        int notice_author_id = Integer.parseInt(request.getSession().getAttribute("U_id").toString());
        Notice notice = new Notice();
        notice.setNotice_title(notice_title);
        notice.setNotice_content(notice_content);
        notice.setNotice_author_id(notice_author_id);
        int result = noticeService.addNotice(notice);

        HashMap<String,Object> map = new HashMap<>();

        if (result == 0){
            map.put("code", 500);
            map.put("msg", "请求失败");
        }else if (result == 1){
            map.put("code", 200);
            map.put("msg", "");
        }

        return map;
    }

    /**
     * 更新公告
     * @param request
     * @return
     */
    @RequestMapping("updateNotice")
    @ResponseBody
    public HashMap updateNotice(HttpServletRequest request){
        int notice_id = Integer.parseInt(request.getParameter("notice_id"));
        String notice_title = request.getParameter("notice_title");
        String notice_content = request.getParameter("notice_content");


        Notice notice = new Notice();
        notice.setNotice_id(notice_id);
        notice.setNotice_content(notice_content);
        notice.setNotice_title(notice_title);
        int result = noticeService.updateNotice(notice);

        HashMap<String,Object> map = new HashMap<>();
        if (result == 1){
            map.put("code", 200);
            map.put("msg", "");
        }else {
            map.put("code", 500);
            map.put("msg", "请求失败");
        }

        return map;
    }


    @RequestMapping("deleteNotice")
    @ResponseBody
    public HashMap deleteNotice(HttpServletRequest request){
        int notice_id = Integer.parseInt(request.getParameter("notice_id"));
        int result = noticeService.deleteNotice(notice_id);
        HashMap<String,Object> map = new HashMap<>();
        if (result == 1){
            map.put("code", 200);
            map.put("msg", "");
        }else {
            map.put("code", 500);
            map.put("msg", "请求失败");
        }
        return map;
    }

}
